#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import asyncio

q = asyncio.queues.Queue(5)


async def producer():
    for i in range(5):
        await asyncio.sleep(1.5)
        await q.put(i)
        print("producer:", i)


async def consumer():
    i = 3
    while True:
        try:
            # msg.get()是阻塞模式，所以使用get_nowait()
            r = q.get_nowait()
            print("consumer:", r)
            i = 3
        except asyncio.queues.QueueEmpty:
            i -= 1
            print("倒计时还剩{}秒".format(i))
            await asyncio.sleep(1)
            if i <= 0:
                print("没有消费，结束")
                break


loop = asyncio.get_event_loop()
tasks = [consumer(), producer()]
loop.run_until_complete(asyncio.wait(tasks))
